import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  SmsAppConfigApi,
  SmsAppConfigReq,
  SmsAppConfigModel,
  SmsAppConfigPage,
  SmsAppConfigList,
  SmsAppConfigKey,
  SmsAppConfigKeyList,
  SmsAppConfigMoveKey,
  SmsAppConfigCreateReq,
  SmsAppConfigUpdateReq,
  EmailBasicReq,
  smsSmsResp,
} from "./types";

class SmsAppConfigApiImpl implements SmsAppConfigApi {
  getKey(model: SmsAppConfigModel): SmsAppConfigKey {
    return {
      configCode: model.configCode,
    } as SmsAppConfigKey;
  }

  // 查询翻页信息消息应用配置
  findPage(query?: SmsAppConfigReq): AxiosPromise<SmsAppConfigPage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasIsDefault = query.isDefault !== undefined;
      query.hasProvider = query.provider !== undefined;
      query.hasConfigType = query.configType !== undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_app_config/page/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询列表信息消息应用配置
  findList(query?: SmsAppConfigReq): AxiosPromise<SmsAppConfigList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_app_config/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询消息应用配置
  findById(query: SmsAppConfigKey): AxiosPromise<SmsAppConfigModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_app_config/id/" + tenant + "/" + query.configCode,
      method: "get",
    });
  }

  // 创建实体消息应用配置
  create(data: SmsAppConfigCreateReq): AxiosPromise<SmsAppConfigModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/sms/sms_app_config",
      method: "post",
      data: data,
    });
  }

  // 修改实体消息应用配置
  update(data: SmsAppConfigUpdateReq): AxiosPromise<SmsAppConfigModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/sms/sms_app_config",
      method: "patch",
      data: data,
    });
  }

  // 上移下移
  move(key: SmsAppConfigKey, up: boolean): AxiosPromise<boolean> {
    const data: SmsAppConfigMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/sms/sms_app_config/move",
      method: "put",
      data: data,
    });
  }



  // 删除消息应用配置
  delete(query: SmsAppConfigKey): AxiosPromise<SmsAppConfigModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_app_config/id",
      method: "delete",
      params: query,
    });
  }

  // 批量删除消息应用配置
  batchDelete(keys: SmsAppConfigKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.configCode);
    const data = {
      tenant: useUserStore().user.tenant,
      configCode: ids,
    } as SmsAppConfigKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/sms/sms_app_config/batch",
      method: "put",
      data: data,
    });
  }

  // 下载导入模板消息应用配置
  downloadTemplate(): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/sms/sms_app_config/" + tenant + "/template.xlsx",
      method: "get",
      responseType: "blob",
      params: {
        tenant: useUserStore().user.tenant,
      },
    });
  }


  // 发送邮件
  sendEmail(data: EmailBasicReq): AxiosPromise<smsSmsResp> {
    data.tenant = useUserStore().user.tenant;
    if(data){
      data.hasProvider=data.provider !== undefined;
    }
    clearEmpty(data);
    return request({
      url: "/v1/sms/email",
      method: "post",
      data: data,
    });
  }

}
const smsAppConfigApi = new SmsAppConfigApiImpl();

export { smsAppConfigApi };
